<?php
class Line_product_reviews_model extends CI_Model
{
    private $table  = 'line_product_reviews';
    private $table1 = 'line_reviews_reply';
    private $table2 = 'line_order_product';
    private $table3 = 'line_base';

    public function getProductReviewsCount($product_id)
    {
        $this->db->where('product_id', $product_id);
        $this->db->where('status', 2); //1：待审核；2：审核通过的；3：未审核通过的
        return $this->db->count_all_results($this->table);
    }

    public function getProductReviewsPage($pageNum, $num, $product_id)
    {
        $this->db->select('line_product_reviews.*,line_reviews_reply.reply_user_name,line_reviews_reply.reply_content');
        $this->db->from($this->table);
        $this->db->join('line_reviews_reply','line_reviews_reply.review_id=line_product_reviews.id','left');
        $this->db->where('product_id', $product_id);
        $this->db->where('status', 2);   //1：待审核；2：审核通过的；3：未审核通过的
        $this->db->limit($pageNum, $num);
        $this->db->order_by('line_product_reviews.id','desc');
        return $this->db->get();
    }
    
    public function validateProductReviews($order_id, $product_id, $uid)
    {
        $this->db->where('order_id', $order_id);
        $this->db->where('product_id', $product_id);
        if (!empty($uid)) {    //供应商需要$uid
            $this->db->where('uid', $uid);
        }
        return $this->db->count_all_results($this->table);
    }

    public function insertLineProductReviews($postData)
    {
        $data = array(
                'order_id'     => $postData['order_id'],
                'product_id'   => $postData['product_id'],
                'product_name' => $postData['product_name'],
                'status' => 1, //待审核
                'score'  => $postData['score'],
                'user_content' => $postData['user_content']
        );
        if (!empty($postData['uid'])) {
            $data['uid'] = $postData['uid'];
        }
        if (!empty($postData['user_name'])) {
            $data['user_name'] = $postData['user_name'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 总页数
     */
    public function reviewTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', $params['status']); //审核通过
        return $this->db->count_all_results();
    }

    /**
     * 查询评论信息
     */
    public function queryReview($params, $start, $count)
    {
        $this->db->select('product_name, score, user_content, line_product_reviews.created_at, line_image');
        $this->db->from($this->table);
        $this->db->join($this->table3,'line_product_reviews.product_id=line_base.id','INNER');
        $this->db->where('line_product_reviews.uid', $params['uid']);
        $this->db->where('line_product_reviews.status', $params['status']); //审核通过
        $this->db->limit($count, $start);
        return $this->db->get();
    }
}